package com.zzyl.aop;


import cn.hutool.core.util.ReflectUtil;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.Joinpoint;
import org.apache.http.util.Args;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;


@Slf4j
@Component
@Aspect
public class AutoFillAspect {

    @Before("@annotation(com.zzyl.aop.AutoFill)")//拦截所有有AutoFill注解的方法
    public void autoFill(JoinPoint joinpoint) throws Throwable {
        Object[] args = joinpoint.getArgs();

        if (args.length != 1) {
            return;
        }

        Object pojo = args[0];
        log.info("方法的原始参数：{}",pojo);
        ReflectUtil.setFieldValue(pojo, "createBy", 1671403256519078138L);
        ReflectUtil.setFieldValue(pojo, "updateBy", 1671403256519078138L);

        ReflectUtil.setFieldValue(pojo, "createTime", LocalDateTime.now());
        ReflectUtil.setFieldValue(pojo, "updateTime", LocalDateTime.now());
        log.info("方法的最终参数：{}",pojo);

    }
}
